package models

//查询
func QueryLgUserAll() [] LgUser {
	data_list := [] LgUser{}
	db.Find(&data_list)
	return data_list
}

//添加
func AddLgUser(obj LgUser) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryLgUserOne(id int) LgUser {
	obj := LgUser{}
	db.First(&obj,id)
	return obj
}

//更新角色
func UpdateLgUser(id int,lguser LgUser) error {
	obj := LgUser{}
	err := db.First(&obj,id).Update(&lguser).Error
	return err
}

//删除
func DeleteLgUser(id int) error {
	obj := LgUser{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}


// 搜索
func QueryLgUserSearch(key string) []LgUser {
	obj := [] LgUser{}
	db.Where("user_name LIKE ?", "%"+key+"%").Find(&obj)
	return obj
}

///////////////////////标签////////////////////////////////

//查询
func QueryLabelAll() [] AssetLabel {
	data_list := [] AssetLabel{}
	db.Find(&data_list)
	return data_list
}

//添加
func AddLabel(obj AssetLabel) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryLabelOne(id int) AssetLabel {
	obj := AssetLabel{}
	db.First(&obj,id)
	return obj
}

//更新角色
func UpdateLabel(id int,label AssetLabel) error {
	obj := AssetLabel{}
	err := db.First(&obj,id).Update(&label).Error
	return err
}

//删除
func DeleteLabel(id int) error {
	obj := AssetLabel{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}


// 搜索
func QueryLabelSearch(key string) []AssetLabel {
	obj := [] AssetLabel{}
	db.Where("label_name LIKE ?", "%"+key+"%").Find(&obj)
	return obj
}


///////////////////////IDC机房////////////////////////////////

//查询
func QueryIdcAll() [] AssetIdc {
	data_list := [] AssetIdc{}
	db.Find(&data_list)
	return data_list
}

//添加
func AddIdc(obj AssetIdc) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryIdcOne(id int) AssetIdc {
	obj := AssetIdc{}
	db.First(&obj,id)
	return obj
}

//更新
func UpdateIdc(id int,label AssetIdc) error {
	obj := AssetIdc{}
	err := db.First(&obj,id).Update(&label).Error
	return err
}

//删除
func DeleteIdc(id int) error {
	obj := AssetIdc{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}


// 搜索
func QueryIdcSearch(key string) []AssetIdc {
	obj := [] AssetIdc{}
	db.Where("idc_name LIKE ?", "%"+key+"%").Find(&obj)
	return obj
}


///////////////////////资产管理////////////////////////////////

//查询
func QueryAssetAll() [] Asset {
	data_list := [] Asset{}
	//一对多查询
	db.Preload("Idc").Preload("Label").Find(&data_list)
	return data_list
}

//查询
func QueryAssetPage(page int) [] Asset {
	data_list := [] Asset{}
	//一对多查询
	db.Preload("Idc").Preload("Label").Limit(12).Offset(12*(page-1)).Find(&data_list)
	return data_list
}

// 搜索
func QueryAssetSearch(key string) []Asset {
	obj := [] Asset{}
	db.Where("host_name LIKE ?", "%"+key+"%").Or("ip LIKE ?", "%"+key+"%").
		Or("inner_ip LIKE ?", "%"+key+"%").Preload("Idc").Preload("Label").Find(&obj)
	return obj
}

// 搜索
func QueryAssetSearchLabel(id int) []Asset {
	obj := [] Asset{}
	db.Where("label_id = ?",id).Where("status = ?",1).Preload("Idc").Preload("Label").Find(&obj)
	return obj
}


//添加
func AddAsset(obj Asset) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryAssetOne(id int) Asset {
	obj := Asset{}
	db.Preload("Idc").Preload("Label").Preload("LgUser").First(&obj,id)
	return obj
}

//更新
func UpdateAsset(id int,asset Asset) error {
	obj := Asset{}
	err := db.First(&obj,id).Update(&asset).Error
	return err
}

//删除
func DeleteAsset(id int) error {
	obj := Asset{}
	db.First(&obj,id).Preload("AssetDevices").Preload("AssetOs").Preload("AssetNic").Preload("AssetPartitions").First(&obj)
	asset_os := AssetOs{}
	asset_nic := AssetNic{}
	asset_partition := AssetPartitions{}
	db.Delete(asset_os,obj.AssetOs)
	db.Delete(asset_nic,obj.AssetNic)
	db.Delete(asset_partition,obj.AssetPartitions)
	err := db.Delete(&obj).Error
	return err
}


//通过ID查询
func QueryAssetDetail(id int) Asset {
	obj := Asset{}
	db.First(&obj,id).Preload("Idc").Preload("Label").Preload("LgUser").Preload("AssetOs").Preload("AssetNic").Preload("AssetPartitions").First(&obj)
	return obj
}


//////////////////////////////////////
//添加
func AddAssetOS(obj AssetOs) error {
	err := db.Create(&obj).Error
	return err
}

func AddAssetNic(obj AssetNic) error {
	err := db.Create(&obj).Error
	return err
}

func AddAssetPart(obj AssetPartitions) error {
	err := db.Create(&obj).Error
	return err
}


//删除
func SyncAsset(id int)  {
	obj := Asset{}
	db.First(&obj,id).Preload("AssetDevices").Preload("AssetOs").Preload("AssetNic").Preload("AssetPartitions").First(&obj)
	asset_os := AssetOs{}
	asset_nic := AssetNic{}
	asset_partition := AssetPartitions{}
	db.Delete(asset_os,obj.AssetOs)
	db.Delete(asset_nic,obj.AssetNic)
	db.Delete(asset_partition,obj.AssetPartitions)
}
